<!--
 * @Author: 孙开源 && sunkaiyuan@namenode.cn
 * @Date: 2023-09-22 10:26:19
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @LastEditTime: 2023-09-22 14:45:43
 * @Description: 
 * 
-->
<template>
  <el-dialog
    draggable
    overflow
    v-model="visible"
    :title="titleMap[mode]"
    :width="400"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <sk-tabs type="border-card">
      <el-tab-pane ref="tab" label="基础信息">
        <el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px">
          <el-row style="display: flex; align-items: center">
            <el-col :span="20">
              <el-form-item label="转账日期" prop="billdate">
                <el-date-picker
                  v-model="form.billdate"
                  type="date"
                  placeholder="选择日期"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                />
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="转账编号" prop="code">
                <el-input
                  v-model="form.code"
                  readonly
                  placeholder="自动生成"
                  class="readonly"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="20">
              <el-form-item label="转出账号" prop="out_subject_id">
                <sk-select-group v-model="form.out_subject_id" :list="accountList" />
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="转入账号" prop="in_subject_id">
                <sk-select-group v-model="form.in_subject_id" :list="accountList" />
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="转出金额" prop="out_amount">
                <el-col :span="11">
                  <el-input v-model="form.out_amount"></el-input>
                </el-col>
                <el-col :span="3" class="text-center">
                  <span>汇率</span>
                </el-col>
                <el-col :span="10">
                  <el-input v-model="form.rate" disabled></el-input>
                </el-col>
              </el-form-item>
            </el-col>

            <el-col :span="20">
              <el-form-item label="转入金额" prop="billdate">
                <el-input v-model="in_amount" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="摘要" prop="summary">
                <el-input
                  v-model="form.summary"
                  :autosize="{ minRows: 4, maxRows: 4 }"
                  type="textarea"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="附件">
        <sk-upload
          v-model="form.attachments"
          :limit="10"
          draggable
          tip="单个文件不要超过10M,请上传图像格式文件"
        >
        </sk-upload>
      </el-tab-pane>
    </sk-tabs>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import skSelectGroup from "@/components/skSelectGroup";
import XEUtils from "xe-utils";
export default {
  emits: ["success", "closed"],
  components: { skSelectGroup },
  data() {
    return {
      mode: "add",
      titleMap: {
        add: "新增",
        edit: "编辑",
      },
      accountList: [],
      form: {
        id: "",
        billdate: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
        code: "",
        rate: 1,
        attachments: [],
      },
      rules: {
        billdate: { required: true, message: "请选择单据日期" },
      },
      visible: false,
      isSaveing: false,
    };
  },
  computed: {
    in_amount() {
      return this.form.out_amount ?? 0 * this.form.rate;
    },
  },
  mounted() {
    this.getAccount();
  },
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;

      this.visible = true;

      return this;
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async valid => {
        if (valid) {
          this.isSaveing = true;
          this.form.in_amount = this.in_amount;
          var res = await this.$API.finance.transfer.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });
    },
    getAccount() {
      this.$API.basedata.subject.list.get({ filter: [{ parent_id: 1002 }] }).then(res => {
        this.accountList = res.data;
        this.accountList.push({ id: 1001, name: "库存现金" });
      });
    },
    //表单注入数据
    setData(data) {
      console.log(data);
      this.form = this.$TOOL.removeNull(data);
    },
  },
};
</script>
<style></style>
